﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Skyper {
	public partial class LoginPage : PhoneApplicationPage {
		#region Constructor
		public LoginPage() {
			InitializeComponent();

			this.Loaded += new RoutedEventHandler(LoginPage_Loaded);
			this.Unloaded += new RoutedEventHandler(LoginPage_Unloaded);
		}
		
		#endregion
		#region Private
		private void loginButton_Click(object sender, RoutedEventArgs e) {
			this.syncProgress.Visibility = System.Windows.Visibility.Visible;
			App.Skype.Sync();
		}
		private void Skype_ConnectionFailure(object sender, EventArgs e) {
			this.syncProgress.Visibility = System.Windows.Visibility.Collapsed;
		}
		private void Skype_ConnectionSuccess(object sender, EventArgs e) {
			this.syncProgress.Visibility = System.Windows.Visibility.Collapsed;
			App.OpenMain();
		}
		private void LoginPage_Unloaded(object sender, RoutedEventArgs e) {
			App.Skype.ConnectionSuccess -= (Skype_ConnectionSuccess);
			App.Skype.ConnectionFailure -= (Skype_ConnectionFailure);
		}
		private void LoginPage_Loaded(object sender, RoutedEventArgs e) {
			if (App.Skype.IsLoggedOn) {
				if (NavigationService.CanGoBack) {
					NavigationService.GoBack();
				} else {
					if (!System.Diagnostics.Debugger.IsAttached) {
						throw new App.QuitException();
					}
				}
			}
			App.Skype.ConnectionSuccess += (Skype_ConnectionSuccess);
			App.Skype.ConnectionFailure += (Skype_ConnectionFailure);
			this.DataContext = App.Skype.Config;
		}
		#endregion
		#region Protected
		#endregion
	}
}